package com.zhongxi.guoyuan.OffLineShop.Adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zhongxi.guoyuan.OffLineShop.Model.ItemShopInfo;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.service.Data;
import com.zhongxi.guoyuan.url.Url;
import com.zhongxi.guoyuan.utils.CommonTool;
import com.zhongxi.guoyuan.utils.DataUtil;
import com.zhongxi.guoyuan.utils.UiUtils;
import com.zhongxi.guoyuan.view.seller.offline.SellerOffLineAdapter;
import com.zhongxi.guoyuan.widget.screenadaptaion.CustomLinearlayout;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

import static com.zhongxi.guoyuan.view.seller.offline.SellerOffLineAdapter.LOADEND;

/**
 * Created by Administrator on 2018/1/11.
 */

public class ShopListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int TYPE_FOOTER = 1;
    private static final int TYPE_ITEM = 2;
    public static final int LOADING = 3;
    public static final int LOADCOMPLENT = 4;
    public static final int LOADEND = 5;
    private List<ItemShopInfo> shopInfoList;
    private int loadingState;

    public ShopListAdapter(List<ItemShopInfo> shopInfoList) {
        this.shopInfoList = shopInfoList;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == shopInfoList.size()) {
            return TYPE_FOOTER;
        } else {
            return TYPE_ITEM;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_FOOTER) {
            return new FooterViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.loadmore_footer, parent, false));
        } else {
            return new StoreViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_shoplist_offline, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, final int position) {
        if (viewHolder instanceof StoreViewHolder) {
            final StoreViewHolder holder = (StoreViewHolder) viewHolder;
            ItemShopInfo mItemShopInfo = shopInfoList.get(position);
            Glide.with(UiUtils.getContext()).load(Url.IMG_URL + mItemShopInfo.getThumbnail()).placeholder(R.mipmap.iv_loading).into(holder.mIvOffline);
            holder.mTvGoodNameOffLine.setText(mItemShopInfo.getName());
            String mDivide = DataUtil.divide(mItemShopInfo.getDistance(), String.valueOf(1000), 2);
            if (DataUtil.moreThanOther(mDivide, "1")) {
                holder.mTvDistance.setText("距您" + mDivide + "km");
            } else {
                holder.mTvDistance.setText("距您" + mItemShopInfo.getDistance() + "m");
            }
            holder.mTvPageview.setText(UiUtils.getContext().getString(R.string.pageview, mItemShopInfo.getOrdernum()));
            holder.mTvAddress.setText(mItemShopInfo.getAddress());
            if (onItemClickListener != null) {
                holder.mRlclick.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        onItemClickListener.onItemClickListener(holder.mRlclick, position);
                    }
                });
            }
        } else if (viewHolder instanceof FooterViewHolder) {
            FooterViewHolder mHolder = (FooterViewHolder) viewHolder;
            switch (loadingState) {
                case LOADING:  // 正在加载
                    mHolder.mLoading.setVisibility(View.VISIBLE);
                    mHolder.mLoadComplent.setVisibility(View.GONE);
                    break;
                case LOADCOMPLENT:  // 加载完成
                    mHolder.mLoading.setVisibility(View.INVISIBLE);
                    mHolder.mLoadComplent.setVisibility(View.GONE);
                    break;
                case LOADEND:  //  记载到底
                    mHolder.mLoading.setVisibility(View.GONE);
                    mHolder.mLoadComplent.setVisibility(View.VISIBLE);
                    break;
            }
        }
    }

    @Override
    public int getItemCount() {
        if (shopInfoList != null && shopInfoList.size() > 0) {
            return shopInfoList.size() + 1;
        }
        return 0;
    }

    /**
     * 加载的状态
     *
     * @param loadingState
     */
    public void setLoadingState(int loadingState) {
        this.loadingState = loadingState;
        notifyDataSetChanged();
    }

    class StoreViewHolder extends RecyclerView.ViewHolder {
        ImageView mIvOffline;
        TextView mTvGoodNameOffLine;
        TextView mTvDistance;
        TextView mTvPageview;
        TextView mTvAddress;
        RelativeLayout mRlclick;
        TextView mTvFlay;

        public StoreViewHolder(View itemView) {
            super(itemView);
            mIvOffline = (ImageView) itemView.findViewById(R.id.iv_offline);
            mTvGoodNameOffLine = (TextView) itemView.findViewById(R.id.tv_goodName_offLine);
            mTvDistance = (TextView) itemView.findViewById(R.id.tv_distance);
            mTvPageview = (TextView) itemView.findViewById(R.id.tv_pageview);
            mTvAddress = (TextView) itemView.findViewById(R.id.tv_address);
            mTvFlay = (TextView) itemView.findViewById(R.id.tv_flay);
            mRlclick = (RelativeLayout) itemView.findViewById(R.id.ll_click);
        }
    }

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClickListener(View view, int position);
    }

    public class FooterViewHolder extends RecyclerView.ViewHolder {

        @Bind(R.id.ll_loading)
        CustomLinearlayout mLoading;
        @Bind(R.id.tv_loadcomplent)
        TextView mLoadComplent;

        public FooterViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
